<?php
// Check permission
global $lock_status, $m, $glo_currency_local;
require_once('quotation_lines.class.php');
$perms =& $AppUI->acl();
$canEdit = $perms->checkModuleItem($m, 'edit');
// Get quote_id from $_GET param
$quote_id = intval(dPgetParam($_GET, "quote_id", 0));
// Get login object
$uid = $AppUI->user_id;
// Prefix of quotations
$prefix = 'qitem_';
// path add Quotation
$pathEditLine = './index.php?m=quotations&a=line_crud&quote_id='.$quote_id;
$pathDeleteLine = './index.php?m=quotations&a=do_line_crud&del=1';
// Object

$vatTypeArr = dPgetSysVal('QItemVATType');
$vatCodeArr = dPgetSysVal('ItemVATCode');

$obj = new CQuotation_lines();
$final = $obj->getListByQuoteId($uid, $quote_id);
$index = 0;
?>
<table width="100%" cellspacing="0" cellpadding="7" border="0" class="infopanel">
<tbody><tr>
	<td>
		<?php if(!$lock_status && $canEdit):?>
		<input type="button" value="New Line" onclick="doCRUDLine(0,<?=$quote_id?>)" class="button"/>
		<?php endif;?>
	</td>
</tr>
</tbody>
</table>
<table id="idLineList" width="100%" cellspacing="0" cellpadding="2" border="0" class="list">
	<tbody>
		<tr>
			<th style="width: 20px;" nowrap="nowrap">&nbsp;</th>
			<th nowrap="nowrap" width="72"><?=$AppUI->_('Code')?></th>
			<th nowrap="nowrap" width="222"><?=$AppUI->_('Name')?></th>
			<th nowrap="nowrap" width="145"><?=$AppUI->_('VAT Type')?></th>
			<th nowrap="nowrap" width="145"><?=$AppUI->_('Apply mode')?></th>
			<th nowrap="nowrap" width="40%"><?=$AppUI->_('Remarks')?></th>
			<th style="width: 20px;">&nbsp;</th>
		</tr>
		<?php if((is_null($final)) or (!is_array($final)) or (sizeof($final) == 0)):?>
		<tr class="second">
			<td colspan="100%">No item available</td>
		</tr>
		<?php else: ?>
		<?php foreach($final as $item):?>
			<?php $index += 1;?>
			<tr class="first">
				<td align="center">
				<?php if(!$lock_status && $canEdit):?>
					<a href="<?=$pathEditLine?>&item_id=<?=intval($item[$prefix.'id'])?>"><img title="<?=$AppUI->_('Edit')?>" src="images/edit.gif" border="0"/></a>
				<?php endif;?>
				</td>
				<td nowrap="nowrap"><?=$item['item_code']?></td>
				<td nowrap="nowrap">
					<?php if(!$lock_status && $canEdit):?>
						<a href="<?=$pathEditLine?>&item_id=<?=intval($item[$prefix.'id'])?>"><b><?=$item['item_name']?></b></a>
					<?php else:?>
						<b><?=$item['item_name']?></b>
					<?php endif;?>
				</td>
				<td align="right">
					<?php if($item['item_vat_code'] < 0):?>
						<div style="float:left"><?=$vatCodeArr[$item['item_vat_code']]?></div>
					<?php else:?>
						<div style="float:left"><?=$vatTypeArr[$item['qitem_vat_type']]?></div>
						<div style="float:right"><?=$vatCodeArr[$item['item_vat_code']]?></div>
					<?php endif;?>
				</td>
				<td nowrap="nowrap">
					<?php
						if(is_null($item['qitem_apply_mode']) or ($item['qitem_apply_mode'] == AUTO_APPLY_VAL)){
							echo $AppUI->_('Automatic');
						}
						else{
							echo "<div style='float:left'>" . $AppUI->_('Manual') ."</div><div style='float:right'><b>" . number_format($item['qitem_fixed_amount']). "</b></div>";
						}
					?>
				</td>
				<td><?=$item['qitem_remarks']?></td>
				<td align="center">
					<?php if(!$lock_status && $canEdit):?>
						<a href="<?=$pathDeleteLine.'&qitem_id='.$item[$prefix.'id']?>" alt="" onclick="return deleteQuotationLine('<?=$item['item_name']?>', <?=$item[$prefix.'id']?>)"><img alt="<?=$AppUI->_('Delete')?>" src="images/delete.gif" border="0"/></a>
					<?php endif;?>
				</td>
			</tr>
			<?php
				$obj = new CQuotation_lines();
				$figureList = array();
				$figureList = $obj->loadFigureList($item[$prefix.'id']);
				$figureNum = count($figureList['qitem_figure_list']);
			?>
			<?php if($figureNum > 0):?>
			<tr class="second">
				<td>&nbsp;</td>
				<td colspan="100%">
					<table width="100%" cellspacing="0" cellpadding="2" border="0" class="list">
						<tr>
							<td width="50px" style="background-color:#e6e6e6">&nbsp;</td>
							<?php for($i = 1; $i <= $figureNum; $i++):?>
							<td style="background-color:#e6e6e6" align="center" width="50px"><b><?=$i?></b></td>
							<?php endfor;?>
							<?php for($i = $figureNum + 1; $i <= 10; $i++):?>
							<td style="background-color:#e6e6e6" align="center" width="50px"><b><?=$i?></b></td>
							<?php endfor;?>
						</tr>
						<tr>
							<td width="50px" style="background-color:#e6e6e6" align="right"><b><?=$AppUI->_('Quantity')?></b></td>
							<?php for($i = 0; $i < $figureNum; $i++):?>
							<td align="right"><?=number_format(@$figureList['qitem_figure_list'][$i], 2)?></td>
							<?php endfor;?>
							<?php for($i = $figureNum + 1; $i <= 10; $i++):?>
							<td align="right">&nbsp;</td>
							<?php endfor;?>
						</tr>
						<tr>
							<td width="50px" style="background-color:#e6e6e6" align="right"><b><?=$AppUI->_('Price')?></b></td>
							<?php for($i = 0; $i < $figureNum; $i++):?>
							<td align="right"><?= currencyFormat(@$figureList['qitem_price_list'][$i], $glo_currency_local)?></td>
							<?php endfor;?>
							<?php for($i = $figureNum + 1; $i <= 10; $i++):?>
							<td align="right">&nbsp;</td>
							<?php endfor;?>
						</tr>
						<tr>
							<td width="50px" style="background-color:#e6e6e6" align="right"><b><?=$AppUI->_('Unit')?></b></td>
							<?php for($i = 0; $i < $figureNum; $i++):?>
							<td align="right"><?=@$figureList['qitem_unit_list'][$i]?></td>
							<?php endfor;?>
							<?php for($i = $figureNum + 1; $i <= 10; $i++):?>
							<td align="right">&nbsp;</td>
							<?php endfor;?>
						</tr>
					</table>
				</td>
			</tr>
			<?php endif;?>
			<?php endforeach;?>
		<?php endif;?>
	</tbody>
</table>
<?php
	if(!is_null($final) and is_array($final) and (count($final) > 0)):?>
	<script>
		// Show button active
		<?php if($lock_status):?>
			<?php if($lock_status==ACTIVE_VAL):?>
				$('btnCompleteStatus').style.visibility = "visible";
			<?php endif;?>
		<?php else:?>
			$('btnActiveStatus').style.visibility = "visible";
			$('btnCompleteStatus').style.visibility = "visible";
		<?php endif;?>
	</script>
<?php endif;?>
<script language="JavaScript" type="text/javascript">
  Table("idLineList").setNormalHover(1);
</script>
